package com.shanghaionstar.carmanage.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.payme.response.CreateOrderResponseType;
import com.shanghaionstar.carmanage.iview.ICarFlowOrderInfoView;
import com.shanghaionstar.carmanage.model.OnstarPayReq;
import com.shanghaionstar.carmanage.model.OrderStatus;
import com.shanghaionstar.carmanage.pay.alipay.AliWebPayRepEntity;
import com.shanghaionstar.carmanage.service.FlowOrderInfoService;
import com.tencent.mm.opensdk.modelpay.PayReq;

import rx.functions.Action1;

/**
 * 4g 套餐数据逻辑处理层
 * Created by Yonglang Hong on 2016/7/27.
 */
public class FlowOrderInfoPresenter extends BasePresenter {

    FlowOrderInfoService orderInfoService;

    ICarFlowOrderInfoView orderInfoView;

    public void FlowOrderInfoPresenter(ICarFlowOrderInfoView orderInfoView) {
        this.orderInfoView = orderInfoView;
    }

    public FlowOrderInfoPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        orderInfoService = new FlowOrderInfoService(context);

    }

    public void setOrderInfoView(ICarFlowOrderInfoView orderInfoView) {
        this.orderInfoView = orderInfoView;
    }

    /**
     * 查询套餐订单信息
     *
     * @param packageTypes
     * @param packageId
     */
    public void loadFlowOrderInfo(PackageType packageTypes, String packageId) {
        subscription = orderInfoService.getFlowPackageOrderInfo(packageTypes, packageId)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<CreateOrderResponseType>() {
                    @Override
                    public void call(CreateOrderResponseType orderInfo) {
                        orderInfoView.showOrderInfo(orderInfo);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        orderInfoView.showErrOnLoadOrder(throwable);
                    }
                });
    }


    public void genSignForOrder(CreateOrderResponseType orderInfo, final String payType) {
        subscription = orderInfoService.genSignReq(orderInfo, payType)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<OnstarPayReq>() {
                    @Override
                    public void call(OnstarPayReq mPayReq) {
                        if(mPayReq != null){
                            PayReq req = new PayReq();
                            req.appId = mPayReq.appId;
                            req.partnerId = mPayReq.partnerId;
                            req.prepayId = mPayReq.prepayId;
                            req.nonceStr = mPayReq.nonceStr;
                            req.timeStamp= mPayReq.timeStamp;
                            req.sign = mPayReq.sign;
                            req.packageValue = mPayReq.packageValue;
                            orderInfoView.genSignReq(req,payType);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        orderInfoView.showErrOnLoadOrder(throwable);
                    }
                });

    }

    /**
     * 查询支付情况
     *
     * @param orderId
     */
    public void queryOrderStatus(String orderId) {
        subscription = orderInfoService.queryOrderStatus(orderId)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<OrderStatus>() {
                    @Override
                    public void call(OrderStatus status) {
                        if (status != null) {
                            orderInfoView.doByOrderPayStatus(status.getStatus());
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        orderInfoView.showErrOnQueryOrderStatus(throwable);
                    }
                });
    }

    /**
     * 查询支付情况
     */
    public void payByAliWeb(CreateOrderResponseType orderInfo) {
        subscription = orderInfoService.payByAliWeb(orderInfo)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<AliWebPayRepEntity>() {
                    @Override
                    public void call(AliWebPayRepEntity webPayRepEntity) {
                        orderInfoView.onAliWebPaySuccess(webPayRepEntity);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        orderInfoView.onAliWebPayFail(throwable);
                    }
                });
    }
}
